package com.kuaiyan.sdkshell.ad;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.kuaiyan.sdkshell.R;
import com.wmcsk.KuaiyanSelectShareSdk;
import com.wmcsk.interfaces.SignActivityStartListener;

import java.io.File;


/**
 * Created by admin on 2018/1/25.
 */
public class ShareWorldActivity extends Activity {

    private TextView txtWord;
    private Button button;
    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.share_world_layout);
        initView();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle =new Bundle();
                bundle.putString("message",txtWord.getText().toString());
                bundle.putBoolean("isShare",true);
                KuaiyanSelectShareSdk.getInstance().startSignActivity(ShareWorldActivity.this,bundle, new SignActivityStartListener() {
                    @Override
                    public void lunchSignActivityError(int i, String s, Exception e) {
                        Log.e("KuaiyanSelectShareSdk","启动失败"+i+"  "+s);
                    }

                    @Override
                    public void lunchSignActivitySuccess() {
                        Log.e("KuaiyanSelectShareSdk","启动成功");
                    }
                });
            }
        });

        //在360自定义系统上可能会存在无法重写menu的问题。如有需要用户可以修改为长按分享。或点击按钮分享。
        txtWord.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                MenuInflater menuInflater = actionMode.getMenuInflater();
                menuInflater.inflate(R.menu.select_menu,menu);
                return true;//返回false则不会显示弹窗
            }

            @Override
            public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
                //根据item的ID处理点击事件
                switch (menuItem.getItemId()){
                    case R.id.select_world:
                        Bundle bundle =new Bundle();
                        bundle.putString("message",txtWord.getText().toString().substring(txtWord.getSelectionStart(), txtWord.getSelectionEnd()));
                        bundle.putBoolean("isShare",true);
                        KuaiyanSelectShareSdk.getInstance().startSignActivity(ShareWorldActivity.this,bundle, new SignActivityStartListener() {
                            @Override
                            public void lunchSignActivityError(int i, String s, Exception e) {
                                Log.e("KuaiyanSelectShareSdk","启动失败"+i+"  "+s);
                            }

                            @Override
                            public void lunchSignActivitySuccess() {
                                Log.e("KuaiyanSelectShareSdk","启动成功");
                            }
                        });
                        actionMode.finish();
                        break;
                }
                return false;//返回true则系统的"复制"、"搜索"之类的item将无效，只有自定义item有响应
            }

            @Override
            public void onDestroyActionMode(ActionMode actionMode) {

            }
        });
    }

    private void initView() {
        button= (Button) findViewById(R.id.share_button);
        txtWord = (TextView) findViewById(R.id.txt_word);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("onActivityResult","requestCode"+requestCode);
        if(requestCode== KuaiyanSelectShareSdk.SHARE_CODE){
            Bundle bundle = data.getExtras();
            boolean shareStatus = bundle.getBoolean("shareStatu");
            Log.e("onActivityResult","shareStatus"+shareStatus);
            if(shareStatus){
                Intent intent2=new Intent(Intent.ACTION_SEND);
                File file = new File(bundle.getString("shareUrl"));
                Uri uri=Uri.fromFile(file);
                intent2.putExtra(Intent.EXTRA_STREAM,uri);
                intent2.setType("image/*");
                startActivity(Intent.createChooser(intent2,"share"));
            }else{
                Log.e("onActivityResult","message"+bundle.getString("message"));
            }

        }
    }
}
